#include <iostream>
#include <fstream>
#include "contacts.pb.h"

const std::string filename = "contacts.bin";

void PrintContacts(contacts::Contacts& contacts)
{
    for (int i = 0; i < contacts.contacts_size(); i++)
    {
        std::cout << "--------------联系人" << i + 1 << "----------------" << std::endl;
        const contacts::PeopleInfo& people = contacts.contacts(i);
        std::cout << "联系人姓名:" << people.name() << std::endl;
        std::cout << "联系人年龄:" << people.age() << std::endl;
        for (int j = 0; j < people.phones_size(); j++)
        {
            const contacts::PeopleInfo_Phone& phone = people.phones(j);
            std::cout << "联系人电话" << j + 1 << ":" << phone.number();
            std::cout << "   (" << contacts::PeopleInfo_Phone_PhoneType_Name(phone.type()) << ")" << std::endl;
        }
        // data被设置了并且是地址类型
        if (people.has_data() && people.data().Is<contacts::Address>())
        {
            contacts::Address address;
            people.data().UnpackTo(&address);
            std::cout << "联系人家庭地址:" << address.home_address() << std::endl;
            std::cout << "联系人单位地址:" << address.unit_address() << std::endl;
        }

        // 方式一
        // if (people.has_qq()){

        // }else if (people.has_wechat()){

        // }

        // 方式二
        switch(people.other_contact_case())
        {
            case contacts::PeopleInfo::OtherContactCase::kQq:
                std::cout << "联系人其他联系方式QQ:" << people.qq() << std::endl;
                break;
            case contacts::PeopleInfo::OtherContactCase::kWechat:
                std::cout << "联系人其他联系方式WeChat:" << people.wechat() << std::endl;
                break;
            default:
                break;
        }

        if (people.remark_size())
        {
            std::cout << "备注信息:" << std::endl;
        }

        for (auto it = people.remark().cbegin(); it != people.remark().cend(); ++it)
        {
            std::cout << "      " << it->first << ":" << it->second << std::endl;
        }
    } 
}

int main()
{
    contacts::Contacts contacts;
    std::fstream input(filename, std::ios::in | std::ios::binary);
    if (!contacts.ParseFromIstream(&input))
    {
        std::cout << "parse error..." << std::endl;
        return 1;
    }

    PrintContacts(contacts);
    return 0;
}